<?php
require_once 'Zend/Db/Table/Abstract.php';
class InfoClassModel extends Zend_Db_Table_Abstract
{
	protected $_name = 'info_class';
	protected $_id = 'id';
	
	/*
	 * Ham gan sinh vien vao lop
	*/
	public function add($sv){
		$model = new InfoClassModel();
		$data = array('id_students'=>$sv['id_students'],
					  'id_subjects'=>$sv['id_subjects']);
		$id = $model->insert($data);
		return $id;
	}
	
	/*
	 * Lay thong tin ve lop-sinhvien
	 */
	public function getStudentClass(){
			$db = Zend_Registry::get('connectDB');
			$mysql = $db ->select()
						->from('info_class')
						->join(array('sv' => 'students'),'sv.id = info_class.id_students',array('name_sv' => 'sv.name',
									 'shsv_sv' => 'sv.shsv','class_sv' => 'sv.class','course_sv' => 'sv.course'))
						->join(array('sj' => 'subjects'),'sj.id = info_class.id_subjects',array('code_sj' => 'sj.code',
									 'name_sj' => 'sj.name'))
						->join(array('r' => 'room'),'r.id = sj.id_room',array('name_r' => 'r.name'))
						->join(array('s' => 'semester'),'s.id = sj.id_semester',array('name_s' => 's.name'));
		//	echo $mysql->__toString();exit();
			$result = $db->fetchAll($mysql);
			return  $result;
	}
	
	/*
	 * Lay thong tin ve lop-sinhvien co gioi han so luong
	*/
	public function getStudentClassLimit($count,$limit){
		$db = Zend_Registry::get('connectDB');
		$mysql = $db ->select()
		->from('info_class')
		->join(array('sv' => 'students'),'sv.id = info_class.id_students',array('name_sv' => 'sv.name',
										 'shsv_sv' => 'sv.shsv','class_sv' => 'sv.class','course_sv' => 'sv.course'))
						->join(array('sj' => 'subjects'),'sj.id = info_class.id_subjects',array('code_sj' => 'sj.code',
									 'name_sj' => 'sj.name'))
						->join(array('r' => 'room'),'r.id = sj.id_room',array('name_r' => 'r.name'))
						->join(array('s' => 'semester'),'s.id = sj.id_semester',array('name_s' => 's.name'))
						->limit($count,$limit);
		//	echo $mySql->__toString();exit();
		$result = $db->fetchAll($mysql);
		return  $result;
	}

	/*
	* Lây danh sách sinh viên có học môn sj_id (sj_id: id của môn học).
	*/

	function getStudentByIdSuject($id){
		$db = Zend_Registry::get('connectDB');
		$mysql = $db ->select()
					 ->from('info_class')
					 ->join(array('sv' => 'students'),'sv.id = info_class.id_students',array('name_sv' => 'sv.name',
										 'shsv_sv' => 'sv.shsv','class_sv' => 'sv.class','course_sv' => 'sv.course'))
						;
		//	echo $mySql->__toString();exit();
		$result = $db->fetchAll($mysql);
		return  $result;
	}
    
    /*Backcx*/
    /* Lấy ra id môn học ứng vs sinh viên */
    function getClassDbbyIdsv($id_sv){
   	    $db = Zend_Registry::get('connectDB');
		$mysql = $db ->select()
					 ->from('info_class')
                     ->where('id_students=?',$id_sv);
						
	
		$result = $db->fetchRow($mysql);
		return  $result;
    }
    /*Lay toan bo bai tap cua sinh vien*/
    function getAllExforStu($id_sv){
        //echo $id_sv;die;
        $db = Zend_Registry::get('connectDB');
		$mysql = $db ->select()
					 ->from('info_class')
					 ->joinLeft('subjects','info_class.id_subjects=subjects.id',array('code','name as sname','weight'))
                     ->joinLeft('exercises','info_class.id_subjects= exercises.id_subjects',array('id as id_e','name as exname','content','time_start','time_end','link'))
                     ->where('id_students=?',$id_sv)
                     ->order('exercises.id DESC');
						
	
		$result = $db->fetchAll($mysql);
		return  $result;
    }
    
}